<?php


namespace app\common\model;

use app\common\service\AddService;
use app\common\service\OrdinaryService;
use app\common\service\ReceiverService;
use think\Db;
use think\Model as ThinkModel;

//项目表
class Project extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__QT_PROJECT__';
    protected $name = 'qt_project';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    public static function getProjectBySort($store_id, $sort)
    {
        return self::where(['store_id' => $store_id, 'sort' => $sort])->find();
    }

    public static function getProjectByName($store_id, $name)
    {
        return self::where(['store_id' => $store_id, 'name' => $name])->find();
    }


    /**
     *   项目列表
     *
     * @param $store_id     门店ID
     * @param $cate_id
     * @param int $is_store_rec
     * @param int $is_user_show
     * @param int $is_free_order
     * @param string $keyword
     * @param int $limit
     * @param string $type 0 全部 1 主项  2  副项
     * @return array
     */
    public function getProjectList($store_id, $cate_id, $is_store_rec = 0, $is_user_show = 0, $is_free_order = 0, $keyword = '', $limit = 10, $type = '0')
    {

        if (!$store_id) {
            return [];
        }

        $where[] = ['store_id', 'eq', $store_id];

        //类型查询
        if (!empty($cate_id)) {
            $where[] = ['project_category_id', 'eq', $cate_id];
        }

        //店长推荐查询
        if (!empty($is_store_rec)) {
            $where[] = ['is_store_recommend', 'eq', 1];
        }
        //是否自由开单
        if (!empty($is_free_order)) {
            $where[] = ['is_free_order', 'eq', 1];
        }

        //是否客人端显示查询
        if (!empty($is_user_show)) {
            $where[] = ['is_user_show', 'eq', 1];
        }

        //关键词查询
        if (!empty($keyword)) {
            $where[] = ['name', 'like', '%' . $keyword . '%'];
        }
        //类型查询
        if (!empty($type)) {
            $where[] = ['type', 'eq', $type];
        }

        $list = $this
            ->where($where)
            ->field('id,store_id,name,price,market_price,time,images')
            ->order('sort asc,id desc')
            ->paginate($limit)
            ->each(function ($item) {
                $images = get_album_array_path($item['images']);
                //取第一个主图
                $item['img'] = !empty($images) ? $images[0] : "";
                return $item;
            });

        return $list->items();
    }

    /**
     *  项目详情
     *
     * @param int $store_id 门店ID
     * @param $project_id    项目ID
     * @return Project
     */
    public function getProjectInfo($store_id = 0, $project_id)
    {
        $where[] = ['id', 'eq', $project_id];
        if (!empty($store_id)) {
            $where[] = ['store_id', 'eq', $store_id];
        }
        $info = $this->where('id', $project_id)->find();
        if ($info) {
            $info['images'] = get_album_array_path($info['images']);
        }

        return $info;
    }

    /**
     * 获取项目图片
     * @param $projectId
     * @return array
     */
    public static function getImages($projectId)
    {
        $info = self::where('id', $projectId)->find();
        if ($info) {
            $info['images'] = get_album_array_path($info['images']);
        }

        return $info['images'];
    }


    /**
     * 计算提成
     * @param $orderData
     * @param $storeInfo
     */
    public static function getCommission($orderData,$storeInfo){
        //更新提成信息
        $project = Db::name('qt_project')->where('id', $orderData['project_id'])->find();
        $commission = 0;
        //普通提成
        if ( $orderData['is_add'] == '1') {
            $commission = new OrdinaryService();
            $commission = $commission->showCommission($orderData['order_type'], $orderData['is_add'], $project);
        }
        //加班提成
        if ( $orderData['is_add'] == '2') {
            $commission = new AddService();
            $commission = $commission->showCommission($orderData['order_type'], $orderData['is_add'], $project);
        }
        //计算接待提成
        $receiver = new ReceiverService();
        $receiver = $receiver->showCommission($orderData['order_type'], $orderData['is_add'], $project);
        if ($storeInfo['floor_commission_type'] == '1') {
            $cashierId = $orderData['sale_id'];
        } else {
            $cashierId = $orderData['creater_room'];
        }
        $insert = [
            'store_id' => $storeInfo['id'],
            'cashier_id' => $cashierId,//接待人
            'user_id' => $orderData['engineer_id'],//技师
            'room_id' => $orderData['room_id'],
            'money' => $orderData['price'],
            'commission' => $commission,//技师提成
            'receiver' => $receiver,//接待提成
            'product_id' => $orderData['project_id'],
            'type' => '1',
            'created_time' => time(),
            'is_pay' => '1',
            'order_project_id' => $orderData['id'],
            'status' => $orderData['order_type'],
            'is_add' => $orderData['is_add'],
        ];
        Db::name('qt_commission_log')->insert($insert);
        //更新订单提成
        Db::name('qt_order_project')->where(['id'=>$orderData['id']])->update(['commission'=>$commission,'receiver'=>$receiver]);
    }

    /**
     * 检测项目是否可以删除
     * @param $projectId [项目ID]
     */
    public static function checkDel($projectId){
        //套餐检测
        $package  = Db::name('qt_data_package_item')->where(['item_id'=>$projectId,'item_type'=>'project'])->find();
        if($package != null){
            return ['code'=>204,'msg'=>'套餐中有此项目，无法删除'];
        }
        //组合下单检测
        $combine  = Db::name('qt_data_combine')->where(['project_id'=>$projectId])->find();
        if($combine != null){
            return ['code'=>204,'msg'=>'组合下单中有此项目，无法删除'];
        }
        //项目减免检测
        $minus  = Db::name('qt_data_minus_item')->where(['item_id'=>$projectId,'item_type'=>'project'])->find();
        if($minus != null){
            return ['code'=>204,'msg'=>'项目减免中有此项目，无法删除'];
        }
        //活动检测
        $activity  = Db::name('qt_data_activity_item')->where(['item_id'=>$projectId,'item_type'=>'project'])->find();
        if($activity != null){
            return ['code'=>204,'msg'=>'活动中有此项目，无法删除'];
        }

        //次卡限制
        $cardNumber  = Db::name('qt_viper_number_card_template_item')
            ->alias('item')
            ->leftJoin('qt_viper_number_card_template t','t.id = item.number_card_template_id')
            ->where(['item_id'=>$projectId,'item_type'=>'project'])->find();
        if($cardNumber != null){
            return ['code'=>204,'msg'=>'次卡【'.$cardNumber['name'].'】有此项目，无法删除'];
        }
        //月卡限制
        $cardMonth  = Db::name('qt_viper_month_card_template_item')
            ->alias('item')
            ->leftJoin('qt_viper_month_card_template t','t.id = item.month_template_id')
            ->where(['item_id'=>$projectId,'item_type'=>'project'])->find();
        if($cardMonth != null){
            return ['code'=>204,'msg'=>'月卡【'.$cardNumber['name'].'】有此项目，无法删除'];
        }

    }



}